package cube.cubetest;

import javax.microedition.khronos.opengles.GL10;

public class CubeStore {
		Cube cubeArray [] [] []; 
		final float gapSize = 1.1f;
		float w;
		float limit;
		float shift =0;
		int last; 
		//Initialise the rubiks cube 
		public CubeStore() { 
			w=1;
			 cubeArray = new Cube[3][3][3]; 
		        for (int i=0; i<3; i++) { 
		        	for (int j=0; j<3; j++) { 
		        		for (int k=0; k<3; k++) { 
		        				cubeArray[i][j][k] = new Cube(); 	
		        		}
		        	}
		        }
		}
		
		//rotate cube clockwise from previous position
		public boolean rotateTop(boolean clockwise, GL10 gl, boolean fixed) { 
			last = 1;
			this.commonSetup(gl);
		        // draw base 
		        for (int i=0; i<3; i++) { 
		        	for(int j=0; j<2; j++) { 
		        		for(int k=0; k<3; k++) { 
		        		    //wrap translate in push/pop so this translation isn't kept
		        			gl.glPushMatrix(); 
		        		    gl.glTranslatef(i*gapSize, j*gapSize, -k*gapSize); 
		        		    cubeArray[i][j][k].draw(gl); 	
		        		    gl.glPopMatrix(); 
		        		}
		        	}
		        }
		        //draw top layer, with rotation 
		        //centre		
				for (int i=-1; i<2; i++) { 
					for (int j=-1; j<2; j++) { 
						//handle centre cube - pure rotation
						if (i==0 && j == 0) { 
							gl.glPushMatrix(); 
							gl.glTranslatef(gapSize, 2*gapSize, -gapSize);
							gl.glRotatef(w+shift, 0, 1, 0);
							cubeArray[1][0][1].draw(gl); 
							gl.glPopMatrix(); 
						}
						gl.glPushMatrix(); 
					    gl.glTranslatef(gapSize, 2*gapSize, -gapSize);
					    gl.glRotatef(w+shift, 0, 1, 0);
					    gl.glTranslatef(i*gapSize, 0, j*gapSize);
					    cubeArray[i+1][2][j+1].draw(gl); 
						gl.glPopMatrix(); 
					}
				} 
				if (!fixed) { 
					if (w<90) { 
						w++; 
					} else { 
						w=0;
						shift = shift+90;
						if (shift==360) { 
							shift=0;
						}
						return true;
					}
				}
				return false;
		}
		
		
		public void drawInit(GL10 gl) {
			last = 0;
			//set-up view
			this.commonSetup(gl);
	        
			//Draw rubiks cube
	        for (int i=0; i<3; i++) { 
	        	for(int j=0; j<3; j++) { 
	        		for(int k=0; k<3; k++) { 
	        		    //wrap translate in push/pop so this translation isn't kept
	        			gl.glPushMatrix(); 
	        		    gl.glTranslatef(i*gapSize, j*gapSize, -k*gapSize); 
	        		    cubeArray[i][j][k].draw(gl); 
	        		    gl.glPopMatrix(); 
	        		}
	        	}
	        }
		}
		
		public void commonSetup(GL10 gl) { 
			gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
	        gl.glMatrixMode(GL10.GL_MODELVIEW);
	        gl.glLoadIdentity();
	        gl.glTranslatef(-0.5f, 0, -5.0f);
	        gl.glRotatef(45,        0, 1, 0);
	        gl.glRotatef(40,        1, 0, 0);
	        gl.glRotatef(30,        0, 0, 1);
		}
		
		public void repeatLast(GL10 gl) { 
			switch (last) { 
			case 0: 
				this.drawInit(gl);
				break; 
			case 1: 
				this.rotateTop(true, gl, true);
				break;
			}
		}


}
